{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.21 \u8bca\u65ad\u5206\u6bb5\u9519\u8bef\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u89e3\u91ca\u5668\u56e0\u4e3a\u67d0\u4e2a\u5206\u6bb5\u9519\u8bef\u3001\u603b\u7ebf\u9519\u8bef\u3001\u8bbf\u95ee\u8d8a\u754c\u6216\u5176\u4ed6\u81f4\u547d\u9519\u8bef\u800c\u7a81\u7136\u95f4\u5954\u6e83\u3002\n\u4f60\u60f3\u83b7\u5f97Python\u5806\u6808\u4fe1\u606f\uff0c\u4ece\u800c\u627e\u51fa\u5728\u53d1\u751f\u9519\u8bef\u7684\u65f6\u5019\u4f60\u7684\u7a0b\u5e8f\u8fd0\u884c\u70b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "faulthandler \u6a21\u5757\u80fd\u88ab\u7528\u6765\u5e2e\u4f60\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\n\u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5f15\u5165\u4e0b\u5217\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import faulthandler\nfaulthandler.enable()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u8fd8\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 -Xfaulthandler \u6765\u8fd0\u884cPython\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 -Xfaulthandler program.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e PYTHONFAULTHANDLER \u73af\u5883\u53d8\u91cf\u3002\n\u5f00\u542ffaulthandler\u540e\uff0c\u5728C\u6269\u5c55\u4e2d\u7684\u81f4\u547d\u9519\u8bef\u4f1a\u5bfc\u81f4\u4e00\u4e2aPython\u9519\u8bef\u5806\u6808\u88ab\u6253\u5370\u51fa\u6765\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "Fatal Python error: Segmentation fault\n\nCurrent thread 0x00007fff71106cc0:\n  File \"example.py\", line 6 in foo\n  File \"example.py\", line 10 in bar\n  File \"example.py\", line 14 in spam\n  File \"example.py\", line 19 in <module>\nSegmentation fault"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8fd9\u4e2a\u5e76\u4e0d\u80fd\u544a\u8bc9\u4f60C\u4ee3\u7801\u4e2d\u54ea\u91cc\u51fa\u9519\u4e86\uff0c\u4f46\u662f\u81f3\u5c11\u80fd\u544a\u8bc9\u4f60Python\u91cc\u9762\u54ea\u91cc\u6709\u9519\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "faulthandler\u4f1a\u5728Python\u4ee3\u7801\u6267\u884c\u51fa\u9519\u7684\u65f6\u5019\u5411\u4f60\u5c55\u793a\u8ddf\u8e2a\u4fe1\u606f\u3002\n\u81f3\u5c11\uff0c\u5b83\u4f1a\u544a\u8bc9\u4f60\u51fa\u9519\u65f6\u88ab\u8c03\u7528\u7684\u6700\u9876\u7ea7\u6269\u5c55\u51fd\u6570\u662f\u54ea\u4e2a\u3002\n\u5728pdb\u548c\u5176\u4ed6Python\u8c03\u8bd5\u5668\u7684\u5e2e\u52a9\u4e0b\uff0c\u4f60\u5c31\u80fd\u8ffd\u6839\u6eaf\u6e90\u627e\u5230\u9519\u8bef\u6240\u5728\u7684\u4f4d\u7f6e\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "faulthandler\u4e0d\u4f1a\u544a\u8bc9\u4f60\u4efb\u4f55C\u8bed\u8a00\u4e2d\u7684\u9519\u8bef\u4fe1\u606f\u3002\n\u56e0\u6b64\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u4f20\u7edf\u7684C\u8c03\u8bd5\u5668\uff0c\u6bd4\u5982gdb\u3002\n\u4e0d\u8fc7\uff0c\u5728faulthandler\u8ffd\u8e2a\u4fe1\u606f\u53ef\u4ee5\u8ba9\u4f60\u53bb\u5224\u65ad\u4ece\u54ea\u91cc\u7740\u624b\u3002\n\u8fd8\u8981\u6ce8\u610f\u7684\u662f\u5728C\u4e2d\u67d0\u4e9b\u7c7b\u578b\u7684\u9519\u8bef\u53ef\u80fd\u4e0d\u592a\u5bb9\u6613\u6062\u590d\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u4e2aC\u6269\u5c55\u4e22\u5f03\u4e86\u7a0b\u5e8f\u5806\u6808\u4fe1\u606f\uff0c\u5b83\u4f1a\u8ba9faulthandler\u4e0d\u53ef\u7528\uff0c\n\u90a3\u4e48\u4f60\u4e5f\u5f97\u4e0d\u5230\u4efb\u4f55\u8f93\u51fa\uff08\u9664\u4e86\u7a0b\u5e8f\u5954\u6e83\u5916\uff09\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}